<?php
/**
 * Plan a new Interview
 * Display a popup form to add a new interview
 * 
 */

use Hive\Ressource\Interview;
use Hive\Ressource;
use Fox\Core\Errors;
use Fox\Dao\SqlFragment;
use Fox\Html\Screen;
use Fox\Popup;
use Fox\Generator;

$xview = Xview::instance();
$rid = $xview->val('rid');
$screen = new Screen('sJob','add',null,true,Generator::ajaxReload('contentCol',
			Generator::url('ressource','interviews',null,array('rid' => $rid))));
$block = $xview->component('ressource','interview',array('rid' => $rid));
$screen->add($block);
if($screen->submitted())
{
	$fields = $screen->get();
	
	if(!$screen->errors())
	{
		try {
			/* @var $contact Hive\Ressource */
			$contact = Xview::instance()->component('ressource', 'object');
			$interview = new Interview();
			$fields['cdate'] = $fields['udate'] = Interview::sqlDateTime($_SERVER['REQUEST_TIME']);
			$interview->setFields($fields);
			$contact->addToInterviews($interview);
			$contact->prepareUpdate(Ressource::CHANGE_INTERVIEW_ADD);
			if($contact->update()) {
				Fox\Message::Success(Msg(Hive::BUNDLE,Hive::MSG_NEW_INTERVIEW_PLANNED));
			}
		}
		catch (Exception $e) {
			Fox\Message::Error($e);
		} 
		catch (Fox\Object\Exception\Save $e) {
			Fox\Message::Error($e);
		} 
	}
	echo $xview->messages(true,true);
	exit;
}
$popup = new Popup('interviewAdd',Msg(Hive::BUNDLE,Hive::BUT_ADD_INTERVIEW),$screen,780);
echo $popup->display();